package pro.uccs.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.web.client.RestTemplate;
import pro.uccs.constant.OpenPlatformConstant;
import pro.uccs.entity.RegisterCallbackParam;
import pro.uccs.util.EncryptUtil;
import pro.uccs.util.HashUtil;
import pro.uccs.util.RestTemplateFactory;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CallbackService {

    public String generateAccessToken() {
        String client_id = OpenPlatformConstant.CLIENT_ID;
        String token="123";
        Long timestamp = 1537509089006l;

        StringBuffer sb = new StringBuffer(token).append("-").append(client_id).append("-").append(timestamp);
        String accessToken = null;
        try {
            accessToken = HashUtil.hash(sb.toString(), "md5");
            System.out.println(accessToken);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return accessToken;
    }

    public String paramsign() {
        String client_id = OpenPlatformConstant.CLIENT_ID;
        Long timestamp = 1537509089006l;
        String token ="123";
        JSONArray event_codes = new JSONArray();
        //event_codes.add("enter_create");
        event_codes.add("enter_buddy_add");
        event_codes.add("enter_buddy_leave");

        String redirect_uri="http://218.104.225.143:10665/test";
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("client_id", client_id);
        paramMap.put("timestamp", timestamp);
        paramMap.put("eventCodes", event_codes);
        paramMap.put("redirectUri", redirect_uri);
        String newParamSign = null;
        try {
            newParamSign =  EncryptUtil.paramObjSign(paramMap,
                    token, "md5");
            System.out.println(newParamSign);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return newParamSign;
    }

    public void register(){
        RestTemplate restTemplate = RestTemplateFactory.getNewInstance();

        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", "application/json");
        headers.add("ACCESS_TOKEN",generateAccessToken());

        List<String> event_codes = new ArrayList<>();
        event_codes.add("enter_buddy_add");
        event_codes.add("enter_buddy_leave");
        /**
         * 构建请求参数
         */
        RegisterCallbackParam registerCallbackParam = new RegisterCallbackParam();
        registerCallbackParam.setClient_id(OpenPlatformConstant.CLIENT_ID);
        registerCallbackParam.setEventCodes(event_codes);
        registerCallbackParam.setRedirectUri("http://218.104.225.143:10665/test");
        registerCallbackParam.setSign(paramsign());
        registerCallbackParam.setTimestamp(1537509089006l);


        HttpEntity<RegisterCallbackParam> r = new HttpEntity<>(registerCallbackParam, headers);
        Object o = restTemplate.postForObject(OpenPlatformConstant.OPEN_CALLBACK_URL, r, Object.class);
        System.out.println(JSON.toJSONString(o));
    }
}
